package org.lanqiao.tjut.Controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lanqiao.tjut.Bean.CourseBean;
import org.lanqiao.tjut.MyUtils.MyUtils;
import org.lanqiao.tjut.Service.ServiceFactory;

public class CourseInfoAddServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 设置编码
		//request.setCharacterEncoding("UTF-8");
		
		// 1、获取表单数据		
		// 2、将保单数据填充到实体类
		CourseBean courseB = (CourseBean) MyUtils.getNewInstance(CourseBean.class);
		
		//id
		courseB.setCouid(MyUtils.obj2Integer(request.getParameter("txtCouId")));
		courseB.setCouname(request.getParameter("txtCouName"));
		//courseB.setTeaid(MyUtils.obj2Int(request.getParameter("txtUserAge")));
		
		// 3、转发Model层进行业务逻辑处理
		int i = ServiceFactory.getUserInfoManagerServiceInstance().saveCouInfo(courseB);
		// 4、根据处理结果进行view层转发，将结果显示给用户客户端
		if(i>0){
			//数据保存成功
			//准备一下在添加成功的页面上进行数据显示
			//获取需要显示的所有的数据集合
			List<CourseBean> lstCou = ServiceFactory.getUserInfoManagerServiceInstance().getAllCouInfo();
			//将需要显示的结果集放入session缓存中
			request.getSession().setAttribute("lstCou", lstCou);
			//清空request缓存中的对象
			request.removeAttribute("courseB");
			//转发到结果显示页面
			request.getRequestDispatcher("logined/gradeAndCourseManager/CourseInfoDetail.jsp").forward(request, response);
		}else{
			//显示错误信息提示页面
			request.getSession().setAttribute("pageErrorInfo", "课程信息添加失败~~~");
			response.sendRedirect("logined/error.jsp");
		}
	}

}
